//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created Modif: 13/03/2005
// Last Modif: 31/08/2005


var XML_DOCS = {};



XML_DOCS._sortdocs=function(d1,d2) {
  if(!d1.getAttribute("CN")) return  1;
  if(!d2.getAttribute("CN")) return -1;
  return d1.getAttribute("CN") - d2.getAttribute("CN");
}
 

function ReorderDocs() {
  var doc_iter= new doc_iterator();
  var tmparr= new Array();
  var root=DOSSIER.selectSingleNode("documents");
  //alert("empty");
  while(GetDocsLength()) {
    tmparr.push(root.removeChild(DOCS[0]));
  }
  //alert("sort");
  tmparr.sort(XML_DOCS._sortdocs);
  //alert("fill");
  for(var n in tmparr) {
    root.appendChild(tmparr[n]);
  }
  //alert("done");
}


function GetDocsLength() {
  return DOCS.length;
}

function AddDoc(pn,title,pd,pri,cited) {
  var node= DOSSIER.selectSingleNode("documents");  
  var PN=DocDBNormalize(pn);
  var doc=GetDocIdx(PN);
  // New doc, create entry
  if(doc< 0) {
    node.appendChild(CreateDoc(PN,title));
    doc=GetDocIdx(PN);
    SetDocCitedNum(PN,doc);
  }
  // Add or update 
  AddDocDB(PN,title,pd,pri);
  UpdatePE(PN);
  if(cited) {
    SetDocD(doc,"yes");
  } else {
    SetDocD(doc,"");
  }
  CaesarImportCitations(GetDossierNumber(),pn);
}

//----------------------------------------------------------------------------------------
function CreateDoc(pn,title) {
  var doc = XMLDOMDOS.createElement("doc");
  var PN  = DocDBNormalize(pn);

  var attr;
  attr = XMLDOMDOS.createAttribute("pn");
  attr.value=PN;
  doc.setAttributeNode(attr);
  
  attr = XMLDOMDOS.createAttribute("P"); // P document 
  attr.value="";
  doc.setAttributeNode(attr);
  
  attr = XMLDOMDOS.createAttribute("D"); // D document
  attr.value="";
  doc.setAttributeNode(attr);
  
  attr = XMLDOMDOS.createAttribute("E"); // E document
  attr.value="";
  doc.setAttributeNode(attr);

    
  return doc;
}


function DeleteDoc(doc) {
  var node= DOSSIER.selectSingleNode("documents");
  node.removeChild(DOCS[doc-1]);
}


//----------------------------------------------------------------------------------------


function FindDocByPN(pn) {
  return DOSSIER.selectSingleNode("documents/doc[ @pn='"+pn+"']");
}

//----------------------------------------------------------------------------------------

function GetDocIdx(pn) {
  for(var idx=1; idx<= GetDocsLength(); idx++) {
    if(GetDocPN(idx)==pn){
      return idx;
    }
  }
  return -1;
}


function SetDocIdx(pn,newidx) {
  var oldidx=GetDocIdx(pn);
  if(!newidx) {
    if(DOCS[oldidx-1].getAttribute("CN") && DOCS[oldidx-1].getAttribute("CV") < GetClaimSetVersion()) {
      alert("Cannot change "+pn+" Dx because it was used as D"+oldidx+" in version #"+
	    DOCS[oldidx-1].getAttribute("CV"));
      return false;
    }
    DOCS[oldidx-1].setAttribute("CN","");
    return true;
  }
  if(oldidx==newidx) {
    return true;
  }
  // Check if target is not already taken 
  if(newidx && DOCS[newidx-1].getAttribute("CN")==newidx && DOCS[newidx-1].getAttribute("CV") < GetClaimSetVersion()) {
    alert("Cannot set "+pn+" as D"+newidx+" because "+DOCS[newidx-1].getAttribute("pn")+" was used as D"+newidx+" in version #"+
	  DOCS[newidx-1].getAttribute("CV"));
    return false;
  }
  if(newidx<= GetDocsLength()) {
    var oldnode =DOCS[oldidx-1].cloneNode(true);
    var newnode =DOCS[newidx-1].cloneNode(true);
    
    DOSSIER.selectSingleNode("documents").replaceChild(oldnode,DOCS[newidx-1]);
    DOSSIER.selectSingleNode("documents").replaceChild(newnode,DOCS[oldidx-1]);    
  }
  return true;
}


//----------------------------------------------------------------------------------------

function GetDocPN(doc) {
  return DOCS[doc-1].getAttribute("pn");
}

function SetDocPN(doc,pn) {
   DOCS[doc-1].setAttribute("pn",pn);
}

//----------------------------------------------------------------------------------------
function GetDocPD(doc) {
  return GetDocDBPD(GetDocPN(doc));
}

function SetDocPD(doc,pd) {
   SetDocDBPD(GetDocPN(doc),pd);
}

//----------------------------------------------------------------------------------------

function GetDocPRI(doc) {
  return GetDocDBPRI(GetDocPN(doc));
}

function SetDocPRI(doc,pri) {  
   SetDocDBPRI(GetDocPN(doc),pri);
}




//----------------------------------------------------------------------------------------
      	 
function UpdatePE(pn) {
 var doc=GetDocIdx(pn);

 var pridate=str2Date(GetDocDBPRI(pn));
 var dospri=str2Date(GetDossierPri());
 var clsetdate=str2Date(GetClaimSetDate());
 var pddate=str2Date(GetDocDBPD(pn));

  
 SetDocP(doc,"");
 SetDocE(doc,"");

 if(pridate && dospri && pddate && clsetdate) {	
   if(pddate > dospri && pddate < clsetdate) { // published between pri and filing dates of app
     alert("P Document"); 
     SetDocP(doc,"yes");
   } else if (pridate < clsetdate && pddate > clsetdate) { // incomplete E implementation !!!!!!!
     alert("E Document");
     SetDocE(doc,"yes");
   }
 }     
}

//----------------------------------------------------------------------------------------



function GetDocTitle(doc) {
  return  GetDocDBTitle(GetDocPN(doc));
}

function SetDocTitle(doc,txt) {
  SetDocDBTitle(GetDocPN(doc),txt);
}

//----------------------------------------------------------------------------------------

function IsDocNotCited(doc) {
  return DOCS[doc-1].getAttribute("CT") || "";
}

function SetDocNotCited(doc,val) {
   DOCS[doc-1].setAttribute("CT",val);
}

//----------------------------------------------------------------------------------------

function IsDocP(doc) {
  return DOCS[doc-1].getAttribute("P") || "";
}

function SetDocP(doc,val) {
   DOCS[doc-1].setAttribute("P",val);
}

//----------------------------------------------------------------------------------------
function IsDocE(doc) {
  return DOCS[doc-1].getAttribute("E") || "";
}

function SetDocE(doc,val) {
   DOCS[doc-1].setAttribute("E",val);
}

//----------------------------------------------------------------------------------------

function IsDocD(doc) {
  return DOCS[doc-1].getAttribute("D") || "";
}

function SetDocD(doc,val) {
   DOCS[doc-1].setAttribute("D",val);
}
//----------------------------------------------------------------------------------------



// Handle Dx 

function is_DocCitedNumUsed(idx) {
  if(idx > GetDocsLength()) {
    return "";
  }
  if(DOCS[idx-1].getAttribute("CN") && DOCS[idx-1].getAttribute("CV") && DOCS[idx-1].getAttribute("CV") < GetClaimSetVersion()) {
    //alert("A: "+idx +" is in use by "+DOCS[idx-1].getAttribute("pn"));
    return DOCS[idx-1].getAttribute("pn");
  }
  //alert("A: " +idx + "not in use");
  return "";
}


function GetDocCitedNum(pn) {
  var node=FindDocByPN(pn);
  return (node && node.getAttribute("CN")) || 0;
}

function SetDocCitedNum(pn,val) {
  var node=0,prev=0;
  var d_iter= new doc_iterator();
  while(!d_iter.end()) {
    // fnd current node with PN==pn
    if(d_iter.getPN()==pn) {
      node=d_iter.getNode();
    }
    // find node with CN == val
    if(d_iter.getCitedNum()==val) {
      if(d_iter.getPN()==pn) { // no changes
	return true;
      }
      prev=d_iter.getNode();
    }
    d_iter.next();
  }
  
  if(prev && prev!=node) {
    if(prev.getAttribute("CV") && prev.getAttribute("CV") < GetClaimSetVersion()) {
      alert("Cannot set "+pn+" as D"+val+" because "+prev.getAttribute("pn")+" was used as D"+val+" in version #"+
	    prev.getAttribute("CV"));
      return false;
    }
  }  
  if(node) {
    node.setAttribute("CN",val);
    node.setAttribute("CV",GetClaimSetVersion());
    if(prev) {
      prev.setAttribute("CN","");
      prev.setAttribute("CV","");
    }
  }
}

function GetDocCitedVersion(pn) {
  var node=FindDocByPN(pn);
  return (node && node.getAttribute("CV")) || 0;
}

// document first cited in version val
function SetDocCitedVersion(pn,val) {
   FindDocByPN(pn).setAttribute("CV",val);
}

//----------------------------------------------------------------------------------------

// Number of documents cited in previous versions 
function GetNumCitedDocs() {
  //alert(DOSSIER.selectNodes("documents/doc[ @CT!='yes' && @CN != '' && @CV !='' && @CV < "+GetClaimSetVersion()+"]").length);
  return DOSSIER.selectNodes("documents/doc[ @CT!='yes' && @CN != '' && @CV !='' && @CV < "+GetClaimSetVersion()+"]").length;
}
	
//----------------------------------------------------------------------------------------

function UpdateDocsFromDB() { // import old format
  for(var d=1; d <= GetDocsLength(); d++) {
    var title=DOCS[d-1].getAttribute("title");
    if(title) {
      AddDocDB(GetDocPN(d),title);
      DOCS[d-1].removeAttribute("title");
    }
  }
}

function doc_iterator(clist,inittab,all) {
  this.base = array_iterator;  
  if(!inittab) {
    var tab=[];
    if(clist) { // limit the list of docs to those used for claims in clist
      var dejavu= new Array();
      var cl_iter=new List_iterator(clist);
      var cl;      
      while(!cl_iter.end()) {
	cl=cl_iter.getValue();
	dejavu[GetClaimDoc_X(cl)]=1;
	dejavu[GetClaimDoc_Y(cl)]=1;
	dejavu[GetClaimDoc_A(cl)]=1;
	if(!isDocpureB(GetClaimDoc_B(cl))) {
	  dejavu[GetClaimDoc_B(cl)]=1;
	}
	cl_iter.next();
      }
      var idx=0;
      for(var dj in dejavu) {
	if(dj) {
	  //alert("Found "+dj+" in "+(GetDocIdx(dj)-1));
	  tab.push(DOCS[GetDocIdx(dj)-1]); // copy nodes 
	}
      }
      //alert("Found "+this.DOCS.length+" docs used in claim(s) "+clist);
    } else {
      if(all) { // take cited and not cited
	tab=DOSSIER.selectSingleNode('documents').childNodes;
      } else {  // take only cited 
	var tmp=DOSSIER.selectSingleNode('documents').childNodes;
	for(var d=0; d<tmp.length;d++) {
	  if(!tmp[d].getAttribute("CT")){
	    tab.push(tmp[d]);
	  }
	}
      }
    }
    this.base(tab);
  } else {
    this.base(inittab);
  }
}

doc_iterator.prototype = new array_iterator;
doc_iterator.prototype.constructor = doc_iterator;
doc_iterator.prototype.getValue    = function()   { return this._idx+1;}
doc_iterator.prototype.seekpn      = function(pn) { 
  this._idx=0;
  while(!this.end() && this.getPN()!=pn) {
    this.next();
  }
}

doc_iterator.prototype.reduce      = function()   { return this; } 
doc_iterator.prototype.iterator    = function()   { return new doc_iterator("",this._tab); }  
doc_iterator.prototype.getNode     = function()   { return this._tab[this._idx];}
doc_iterator.prototype.getPN       = function()   { return this._tab[this._idx].getAttribute("pn");}
doc_iterator.prototype.setPN       = function(pn) { this._tab[this._idx].setAttribute("pn",pn);}
doc_iterator.prototype.isNotCited  = function()   { return this._tab[this._idx].getAttribute("CT")|| "";} 
doc_iterator.prototype.isP         = function()   { return this._tab[this._idx].getAttribute("P") || "";}
doc_iterator.prototype.isE         = function()   { return this._tab[this._idx].getAttribute("E") || "";}   
doc_iterator.prototype.isD         = function()   { return this._tab[this._idx].getAttribute("D") || "";}
doc_iterator.prototype.getCitedNum = function()   { return this._tab[this._idx].getAttribute("CN") || "";}
doc_iterator.prototype.setCitedNum = function(val){ 
  this._tab[this._idx].setAttribute("CN",val) ;
  this._tab[this._idx].setAttribute("CV",GetClaimSetVersion());
}
doc_iterator.prototype.getCitedVer = function()   { 
  return parseInt(this._tab[this._idx].getAttribute("CV")) || 1;
}
doc_iterator.prototype.getPD       = function()   {  return GetDocDBPD(this.getPN());}
doc_iterator.prototype.getPri      = function()   {  return GetDocDBPRI(this.getPN());}
doc_iterator.prototype.getTitle    = function()   {  return GetDocDBTitle(this.getPN());}
doc_iterator.prototype.getRef      = function()   {  return "D"+this.getCitedNum();}

doc_iterator.prototype.RefList     = function() {
  var lout=new List();
  for (var i in this._tab) {    
    if(this._tab[i].getAttribute("CN")>=1) {
      lout.push("D"+this._tab[i].getAttribute("CN"));    
    }
  }
  return lout;
}

doc_iterator.prototype.getText=function(tand) {
  return list_totext(iter_mapreduce(this, function(x) { return "D"+x;}, function(o,e) { if(o) return o+","+e; return e}),tand);
}

doc_iterator.prototype.getcText=function(tand,tto) {
  return this.getText(tand);
}

